package com.smart.campus.school.tcp;

import cn.hutool.http.HttpUtil;
import com.smart.campus.common.core.domain.AjaxResult;
import com.smart.campus.common.utils.DateUtils;
import com.smart.campus.framework.web.service.DictService;
import com.smart.campus.school.domain.AttendanceRecord;
import com.smart.campus.school.domain.ScBadge;
import com.smart.campus.school.domain.SchoolExt;
import com.smart.campus.school.service.*;
import com.smart.campus.system.service.ISysDeptService;
import com.smart.campus.system.service.ISysNoticeService;
import com.smart.campus.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class TcpDataService {

    @Autowired
    private IScBadgeService badgeService;

    @Autowired
    private IAttendanceRecordService attendanceRecordService;

    public void sendDataToServer(String data){
        try {
            if(StringUtils.isNoneEmpty(data)){
                int totalLength = data.length();
                System.out.println("totalLength:"+totalLength);
                int headLength  = 1;
                int deviceNoLength  = 8;
                int deviceDateLength  = 12;
                int cardNoLength  = 10;
                String head = data.substring(0,headLength);
                String deviceNo = data.substring(headLength,headLength+deviceNoLength);
                String deviceDate= data.substring(headLength+deviceNoLength,headLength+deviceNoLength+deviceDateLength);
                Date time =  DateUtils.dateTime("yyMMddHHmmss",deviceDate);
                String deviceTime = DateUtils.parseDateToStr("yyyyMMddHHmmss",time);
                String cardNo = data.substring(headLength+deviceNoLength+deviceDateLength,headLength+deviceNoLength+deviceDateLength+cardNoLength);
                int lastDeviceNo = Integer.parseInt(deviceNo, 16);
                log.info("考勤卡-head:{},deviceNo:{},deviceTime{},cardNo{},lastDeviceNo{}",head,deviceNo,deviceTime,cardNo,lastDeviceNo);
               /* String url = "http://localhost/api/saveAttendance";
                Map<String, Object> paramMap = new HashMap<>();
                paramMap.put("deviceNo",lastDeviceNo);
                paramMap.put("cardNo",cardNo);
                paramMap.put("deviceTime",deviceTime);
                paramMap.put("inOutFlag","0");
                String result = HttpUtil.get(url,paramMap);*/
                saveAttendance(String.valueOf(lastDeviceNo),cardNo,deviceTime,"0");
                //log.info("netty 发送打卡记录结果：{}",result);
            }
        } catch (Exception e) {
            log.error("netty 发送打卡记录差异长：参数{}",data,e);
        }

    }
    public void saveAttendance(String deviceNo, String cardNo, String deviceTime, String inOutFlag)
    {
        log.info("打卡记录：-------------------->deviceNo=" + deviceNo + "&cardNo=" + cardNo + "&deviceTime=" + deviceTime + "&inOutFlag=" + inOutFlag);

        if("0".equals(deviceNo) || "0000000000".equals(cardNo)){
            return ;
        }
        setClockData(deviceNo,cardNo,deviceTime,inOutFlag);
    }

    /**
     * 设置考勤记录
     * @param deviceNo
     * @param cardNo
     * @param deviceTime
     * @param inOutFlag
     */
    private  void setClockData(String deviceNo,String cardNo,String deviceTime,String inOutFlag){
        //0000000000 无效卡号 心跳数据
        //获取学校编号
        String schoolNo = "1001";//deviceNo.substring(0,3);
        ScBadge query = new ScBadge();
        query.setBadgeNo(cardNo);
        query.setScId(118L);
        List<ScBadge> badgeList = badgeService.selectScBadgeList(query);
        if(CollectionUtils.isNotEmpty(badgeList)) {
            //插入打卡记录
            ScBadge scBadge = badgeList.get(0);
            AttendanceRecord record = new AttendanceRecord();
            record.setCardNo(scBadge.getBadgeNo());
            record.setDeviceNo(deviceNo);
            record.setSchoolId(118L);
            record.setCreateTime(DateUtils.dateTime("yyyyMMddHHmmss", deviceTime));
            attendanceRecordService.insertAttendanceRecord(record);

        }

    }

    public static void main(String[] args) {
                     //#1902911F00000000000000000000000000000C0
                     //#000000042011302011270000000000A000010C3
        String data = "#000000042011302011270000000000A000010C3";
        //deviceNo=4&cardNo=0000000000&deviceTime=20201130201127&inOutFlag=0
        int totalLength = data.length();
        System.out.println("totalLength:"+totalLength);
        int headLength  = 1;
        int deviceNoLength  = 8;
        int deviceDateLength  = 12;
        int cardNoLength  = 10;
        int flagLength  = 1;
        int dataLength  = 2;
        int historyLength  = 4;
        int validateSumLength  = 2;

        String head = data.substring(0,headLength);

        String deviceNo = data.substring(headLength,headLength+deviceNoLength);

        String deviceDate= data.substring(headLength+deviceNoLength,headLength+deviceNoLength+deviceDateLength);
        Date time =  DateUtils.dateTime("yyMMddHHmmss",deviceDate);
        String attTime = DateUtils.parseDateToStr("yyyyMMddHHmmss",time);
        String cardNo = data.substring(headLength+deviceNoLength+deviceDateLength,headLength+deviceNoLength+deviceDateLength+cardNoLength);
        int lastDeviceNo = Integer.parseInt(deviceNo, 8);

        log.info("考勤卡-head:{},deviceNo:{},attTime{},cardNo{},lastDeviceNo{}",head,deviceNo,attTime,cardNo,lastDeviceNo);

    }
}
